package com.hwg.emp.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 过滤请求
 *
 */
public class LoginFilter implements Filter{

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain)
			throws IOException, ServletException {
		  HttpServletRequest req=(HttpServletRequest)arg0;
		  HttpServletResponse resp=(HttpServletResponse)arg1;
		  HttpSession session=req.getSession();
		  if(session.getAttribute("USER")==null&&(req.getServletPath().indexOf("/login")<0)){
			  //req.getRequestDispatcher("/login.jsp").forward(req, resp);
			  resp.sendRedirect(req.getContextPath()+"/login.jsp");
		  }else{
			  chain.doFilter(req, resp);
		  }
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
